package com.xcjw.com.Activity;

import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xcjw.com.Base.Myapplication;
import com.xcjw.com.Bean.LoginUser;
import com.xcjw.com.Bean.MonthPlanbean;
import com.xcjw.com.Http.HttpUrl;
import com.xcjw.com.R;
import com.xcjw.com.Utils.FileUtils;
import com.xcjw.com.Utils.JsonUtil;
import com.xcjw.com.Utils.StatusBarUtil;
import com.xcjw.com.Utils.TimeFormatUtils;
import com.xcjw.com.Utils.ToastUtils;
import com.xcjw.com.View.MyListview;
import com.xcjw.com.View.ObservableScrollView;
import com.xcjw.com.View.SimpleTool;
import com.xcjw.com.View.StatusBarHeightView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @packname com.xcjw.com.Activity
 * @filename MonThActivity
 * @date on 2018/12/7 16:23
 *****/
public class MonThActivity extends AppCompatActivity implements View.OnClickListener, ObservableScrollView.OnObservableScrollViewListener {

      @BindView(R.id.main_layout)
      RelativeLayout mainLayout;

      @BindView(R.id.listview)
      MyListview listview;
      @BindView(R.id.txt_left_title)
      TextView txtLeftTitle;
      @BindView(R.id.txt_main_title)
      TextView txtMainTitle;
      @BindView(R.id.txt_right_titleS)
      TextView txtRightTitleS;
      @BindView(R.id.txt_right_title)
      TextView txtRightTitle;
      @BindView(R.id.simple_toolbar)
      SimpleTool simpleToolbar;
      @BindView(R.id.layout)
      StatusBarHeightView layout;
      @BindView(R.id.scroll)
      ObservableScrollView scroll;
      @BindView(R.id.smart)
      SmartRefreshLayout smart;
      @BindView(R.id.image)
      ImageView image;
      @BindView(R.id.txt_left_title1)
      TextView txtLeftTitle1;
      @BindView(R.id.txt_main_title1)
      TextView txtMainTitle1;
      @BindView(R.id.txt_right_titleS1)
      TextView txtRightTitleS1;
      @BindView(R.id.txt_right_title1)
      TextView txtRightTitle1;
      @BindView(R.id.simple_toolbar1)
      SimpleTool simpleToolbar1;

      private LoginUser.ContentBean user;
      private int result;
      private String id;

      private int heights;
      private int height1;
      private View headerview;
      List<MonthPlanbean.ContentBean> list;
      private MonthPlanAdapter adapter;
      private int intExtra;
      private TextView main;
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
            StatusBarUtil.setRootViewFitsSystemWindows(this, false);
            StatusBarUtil.setTranslucentStatus(this);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.month_activity);
            ButterKnife.bind(this);
            Myapplication.getInstance().addActivity(this);
            user = (LoginUser.ContentBean) FileUtils.getObject(MonThActivity.this, "loginUser");
            if (TextUtils.isEmpty(user.getNeighborhoodCommittees())) {
                  txtRightTitle.setText("");
            } else {
                  txtRightTitle.setText(user.getNeighborhoodCommittees());

            }
            list = new ArrayList<>();
            result = 0;
            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                  result = this.getResources().getDimensionPixelSize(resourceId);
            }
            headerview = View.inflate(this, R.layout.month_header, null);
            id = getIntent().getStringExtra("id");
            list = new ArrayList<>();
            main = (TextView) headerview.findViewById(R.id.main_content);
            txtLeftTitle.setOnClickListener(this);
            txtLeftTitle1.setOnClickListener(this);
            intExtra = getIntent().getIntExtra("id", 0);
            WindowManager wm = this.getWindowManager();
            Point point = new Point();
            wm.getDefaultDisplay().getSize(point);
            int width = point.x;
            heights = point.y;
            listview.addHeaderView(headerview);
            adapter = new MonthPlanAdapter();
            listview.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            smart.autoRefresh();
            ViewTreeObserver viewTreeObserver = layout.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {
                        layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        height1 = layout.getHeight();//这里取的高度应该为图片的高度-标题栏
                        //注册滑动监听
                        scroll.setOnObservableScrollViewListener(MonThActivity.this);
                  }
            });
            smart.setOnRefreshListener(new OnRefreshListener() {
                  @Override
                  public void onRefresh(RefreshLayout refreshlayout) {
                        list.clear();
                        getdate();
                  }
            });

            txtLeftTitle.setOnClickListener(this);
            txtLeftTitle1.setOnClickListener(this);

      }

      private void getdate() {
            OkHttpUtils.get(HttpUrl.Url + HttpUrl.MONTHQUERY).params("arrangeCheckId", id).execute(new StringCallback() {
                  @Override
                  public void onSuccess(String s, Call call, Response response) {
                        if (response.code() == 200) {
                              final MonthPlanbean planbean = JsonUtil.parseJsonToBean(s, MonthPlanbean.class);
                              if (planbean.getCode().equals("0")) {
                                    list.addAll(planbean.getContent());
                                    if (planbean.getContent().size() > 0) {
                                          if (!TextUtils.isEmpty(planbean.getContent().get(0).getShowContent()))
                                                main.setText(planbean.getContent().get(0).getShowContent());
                                    }
                                    adapter.notifyDataSetChanged();
                                    smart.finishRefresh();
                              } else {
                                    smart.finishRefresh();
                                    ToastUtils.shorttoast(MonThActivity.this, planbean.getMessage());
                              }

                        } else {
                              smart.finishRefresh();
                              ToastUtils.shorttoast(MonThActivity.this, "网络繁忙，稍后重试");
                        }

                        if (TextUtils.isEmpty(main.getText().toString())){
                              headerview.setVisibility(View.GONE);
                        }else {
                              headerview.setVisibility(View.VISIBLE);
                        }

                        adapter.notifyDataSetChanged();

                  }

                  @Override
                  public void onError(Call call, Response response, Exception e) {
                        smart.finishRefresh();
                        ToastUtils.shorttoast(MonThActivity.this, "网络繁忙，稍后重试");

                  }
            });
      }

      @Override
      public void onClick(View v) {
            switch (v.getId()) {
                  case R.id.txt_left_title:
                        onBackPressed();
                        break;
                  case R.id.txt_left_title1:
                        onBackPressed();
                        break;

            }

      }

      @Override
      public void onObservableScrollViewListener(int l, int t, int oldl, int oldt) {
            System.out.println("滑动" + t + "===" + height1);
            if (t > 0 && t < height1) {
                  simpleToolbar1.setVisibility(View.GONE);
                  image.setVisibility(View.GONE);
            } else if (t >= height1) {
                  image.setVisibility(View.VISIBLE);
                  RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) image.getLayoutParams();
                  params.height = result;
                  params.width = heights;
                  image.setLayoutParams(params);
                  simpleToolbar1.setVisibility(View.VISIBLE);
                  simpleToolbar1.setBackgroundColor(getResources().getColor(R.color.blue));
            }
      }

      class MonthPlanAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                  return list.size() > 0 ? list.size() : 0;
            }

            @Override
            public Object getItem(int position) {
                  return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                  return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                  MonthHolder holder = null;
                  if (convertView == null) {
                        holder = new MonthHolder();
                        convertView = View.inflate(MonThActivity.this, R.layout.item_monthplan_layout, null);
                        holder.time = (TextView) convertView.findViewById(R.id.week_day);
                        holder.xcontent = (TextView) convertView.findViewById(R.id.xcontent);
                        convertView.setTag(holder);
                  } else {
                        holder = (MonthHolder) convertView.getTag();
                  }
                  holder.time.setText(TimeFormatUtils.ms2DIAN(list.get(position).getDoingtime()).substring(5));
                  holder.xcontent.setText(list.get(position).getContents());
                  return convertView;
            }
      }

      class MonthHolder {
            TextView time, xcontent;
      }
}